import { getBannerList, getPlayList, getSongMenuList } from "../../services/index"
import menuStore from "../../stores/menuStore";
import musicStore from "../../stores/musicStore";


Page({
  data: {
    searchValue:'',
    banners:[],
    recommendMusicList:[], // 推荐歌曲
    hotMenuList:[], // 热门歌单
    officialMenuList:[], // 官方歌单
    rankingInfos:{}, // 巅峰榜
  },
  onLoad(options){
    this.fetchBannerList();
    // this.fetchPlayList();
    musicStore.onState("recommendMusicList",(value)=>{
      if (!value.tracks) return
      this.setData({
        recommendMusicList:value?.tracks?.slice(0,6)
      })
    })
    musicStore.dispatch('fetchPlayListAction', '3778678')
    this.fetchSongMenuList();
    menuStore.dispatch('fetchRankingAction')
    menuStore.onState("upRanking",this.handleRankingData("upRanking"))
    menuStore.onState("originRanking",this.handleRankingData("originRanking"))
    menuStore.onState("newRanking",this.handleRankingData("newRanking"))
  },
  handleRankingData(key){
    return (value)=>{
      // console.log(value)
      if (!value.name) return;
      const newRankingInfos = { ...this.data.rankingInfos, [key]: value }
      this.setData({ rankingInfos: newRankingInfos })
    }
  },

  async fetchBannerList(){
    const res = await getBannerList()
    this.setData({banners:res.banners})
  },
  // 推荐歌曲
  // async fetchPlayList(){
  //   const res = await getPlayList(2884035)
  //   const recommendMusicList = res.playlist?.tracks?.slice(0,6);
  //   this.setData({recommendMusicList})
  // },
  // 歌单（热门、官方）
  fetchSongMenuList(){
    getSongMenuList('热门').then(res=>{
      this.setData({hotMenuList:res.playlists})
    })
    getSongMenuList('官方').then(res=>{
      this.setData({officialMenuList:res.playlists})
    })
  },

  // 跳转搜索
  onClickInput(){
    wx.navigateTo({
      url: '/pages/search-music/search-music'
    })
  },
  bannerImgLoad(event){
    // console.log(event)
  },
  onShareAppMessage(){

  },
  onRecommendMoreTap(){
    wx.navigateTo({
      url: '/pages/detail-music/detail-music',
    })
  },
  

})
